package clids.ex2.actions;

import java.io.File;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.FileInputStream;

import java.io.IOException;

import clids.ex2.filescript.ToolBox;

public class ActionCopy extends AbstractAction {

	public void doAction (String filePath) {
		
		//the directory of the file that will be copied.
		//actionParam is the target dir.
		String targetFileDir = ToolBox.getSourceDir() + actionParam;
		
		//TODO - need to check if actionParam starts with "/"
		
		new File(targetFileDir).mkdirs();  //creating dir. structure.
		
		String targetFilePath = targetFileDir + new File(filePath).getName();
		
		copyFiles(filePath, targetFilePath);	
		
	}

	private void copyFiles(String sourcePath, String targetPath) {
		
		try {
			
			OutputStream output = new FileOutputStream(targetPath);
			InputStream input = new FileInputStream(sourcePath);
				
			int result;
				
			while ((result = input.read()) != -1) {
				
				output.write(result);
			}
			
			input.close();
			output.close();
			
		} catch (IOException io) {
			
			System.err.println("Couldn't copy file"); //shouldn't be here.
		} 
		
	}
	
	
}
